﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;


namespace TinyCinema.Bussiness.HumanResrouces
{
    public class Employees:DependencyObject
    {
        public static DependencyProperty lstEmployeeProperty = DependencyProperty.Register("lstEmployee", typeof(ObservableCollection<Data.Employee>), typeof(HumanResrouces.Employees));
        public ObservableCollection<Data.Employee> lstEmployee
        {
            get { return (ObservableCollection<Data.Employee>)GetValue(lstEmployeeProperty); }
            set { SetValue(lstEmployeeProperty, value); }
        }
        //public static DependencyProperty lstManagerProperty = DependencyProperty.Register("lstManager", typeof(ObservableCollection<Data.Employee>), typeof(HumanResrouces.Employees));
        //public ObservableCollection<Data.Employee> lstManager
        //{
        //    get { return (ObservableCollection<Data.Employee>)GetValue(lstManagerProperty); }
        //    set { SetValue(lstManagerProperty, value); }
        //}
        public static DependencyProperty IsEditableProperty = DependencyProperty.Register("IsEditable", typeof(bool), typeof(HumanResrouces.Employees));
        public bool IsEditable
        {
            get { return (bool)GetValue(IsEditableProperty); }
            set
            {
                SetValue(IsEditableProperty, value);
            }
        }
        public static DependencyProperty IsViewModeEnableProperty = DependencyProperty.Register("IsViewModeEnable", typeof(bool), typeof(Employees));
        public bool IsViewModeEnable
        {
            get { return (bool)GetValue(IsViewModeEnableProperty); }
            set
            {
                SetValue(IsViewModeEnableProperty, value);
            }
        }
        public static DependencyProperty CurrentViewProperty = DependencyProperty.Register("CurrentView", typeof(Data.Employee), typeof(Employees));
        public Data.Employee CurrentView
        {
            get { return (Data.Employee)GetValue(CurrentViewProperty); }
            set
            {
                SetValue(CurrentViewProperty, value);
                if ((value as Data.Employee).ID == null) IsEditable = true;
                else IsEditable = false;
            }
        }
        public static DependencyProperty CurrentViewIndexProperty = DependencyProperty.Register("CurrentViewIndex", typeof(int), typeof(HumanResrouces.Employees));
        public int CurrentViewIndex
        {
            get { return (int)GetValue(CurrentViewIndexProperty); }
            set
            {
                if (value < 1 || value > lstEmployee.Count) return;
                try { CurrentView = lstEmployee[value - 1]; }
                catch { return; }
                SetValue(CurrentViewIndexProperty, value);
            }
        }
        public static DependencyProperty strSearchProperty = DependencyProperty.Register("strSearch", typeof(string), typeof(HumanResrouces.Employees));
        public string strSearch
        {
            get { return (string)GetValue(strSearchProperty); }
            set
            {
                SetValue(strSearchProperty, value);
            }
        }
        public static DependencyProperty IsSearchingProperty = DependencyProperty.Register("IsSearching", typeof(bool), typeof(HumanResrouces.Employees));
        public bool IsSearching
        {
            get { return (bool)GetValue(IsSearchingProperty); }
            set
            {
                SetValue(IsSearchingProperty, value);
            }
        }
        public static DependencyProperty SearchFilterProperty = DependencyProperty.Register("SearchFilter", typeof(ObservableCollection<String>), typeof(HumanResrouces.Employees));
        public ObservableCollection<String> SearchFilter
        {
            get { return (ObservableCollection<String>)GetValue(SearchFilterProperty); }
            set { SetValue(SearchFilterProperty, value); }
        }
        public static DependencyProperty SearchFilterIndexProperty = DependencyProperty.Register("SearchFilterIndex", typeof(int), typeof(HumanResrouces.Employees));
        public int SearchFilterIndex
        {
            get { return (int)GetValue(SearchFilterIndexProperty); }
            set { SetValue(SearchFilterIndexProperty, value); }
        }
        public static DependencyProperty IsCreatingDepartmentProperty = DependencyProperty.Register("IsCreatingDepartment", typeof(bool), typeof(Employees));
        public bool IsCreatingDepartment
        {
            get { return (bool)GetValue(IsCreatingDepartmentProperty); }
            set
            {
                SetValue(IsCreatingDepartmentProperty, value);
                IsEditable = value;
            }
        }
        public static DependencyProperty IsCreatingPositionProperty = DependencyProperty.Register("IsCreatingPosition", typeof(bool), typeof(Employees));
        public bool IsCreatingPosition
        {
            get { return (bool)GetValue(IsCreatingPositionProperty); }
            set
            {
                SetValue(IsCreatingPositionProperty, value);
                IsEditable = value;
            }
        }
        public static DependencyProperty IsCreatingProperty = DependencyProperty.Register("IsCreating", typeof(bool), typeof(Employees));
        public bool IsCreating
        {
            get { return (bool)GetValue(IsCreatingProperty); }
            set
            {
                SetValue(IsCreatingProperty, value);
                IsEditable = value;
            }
        }
        public ICommand ChooseEmployeeCommand { get; set; }
        public ICommand CreateCommand { get; set; }
        public ICommand EditCommand { get; set; }
        public ICommand BackCommand { get; set; }
        public ICommand SaveCommand { get; set; }
        public ICommand DeleteCommand { get; set; }
        public ICommand AddPhotoCommand { get; set; }
        public ICommand PreviousCommand { get; set; }
        public ICommand NextCommand { get; set; }
        public ICommand GoToPageCommand { get; set; }
        public ICommand SearchCommand { get; set; }
        public ICommand SearchCancelCommand { get; set; }
        public ICommand AddDepartmentCommand { get; set; }
        public ICommand AddPositionCommand { get; set; }
        public ICommand ResetPasswordCommand { get; set; }
        public Messenger Messenger { get; set; }
       
        public Employees()
        {
            lstEmployee = Data.Employee.GetAllEmployee();
            IsEditable = true; // Disable nút Edit
            IsViewModeEnable = true;
            Messenger = new Bussiness.Messenger();
            IsCreating = false;
            ChooseEmployeeCommand = new RelayCommand<Data.Employee>(
                (s) => true,
                (s) =>
                {
                    CurrentViewIndex = lstEmployee.IndexOf(s) + 1;
                    CurrentView = s;
                    IsEditable = false;
                    IsViewModeEnable = false;
                }
            );
            CreateCommand = new RelayCommand<Data.Employee>(
                (s) => IsViewModeEnable,
                (s) =>
                {
                    CurrentView = new Data.Employee();
                    IsEditable = true;
                    IsViewModeEnable = false;
                    IsCreating = true;
                }
            );
            EditCommand = new RelayCommand<Data.Employee>(
                (s) => !IsEditable && !IsViewModeEnable,
                (s) =>
                {
                    IsEditable = true;
                }
            ); 
            BackCommand = new RelayCommand<Data.Employee>(
                 (s) => true,
                 (s) =>
                 {
                     if (IsCreatingDepartment)
                     {
                         IsCreatingDepartment = false;
                         IsEditable = true;
                         return;
                     }
                     if (IsCreatingPosition)
                     {
                         IsCreatingPosition = false;
                         IsEditable = true;
                         return;
                     }
                     if (IsEditable)
                     {
                         var message1 = new Message.BackConfimation();
                         Messenger.Send(message1);
                         if (message1.Result == MessageBoxResult.Yes)
                         {
                             Messenger.Send(new Message.BackConfimationOK());
                             IsViewModeEnable = true;
                             IsEditable = true;
                             if (lstEmployee.IndexOf(CurrentView) == -1) return;
                             lstEmployee[CurrentViewIndex - 1] = new Data.Employee(CurrentView.ID);
                             CurrentView = lstEmployee[CurrentViewIndex - 1];
                         }
                     }
                     else
                     {
                         IsViewModeEnable = true;
                         IsEditable = true;
                         Messenger.Send(new Message.BackConfimationOK());
                     }                     
                 }
             );
            SaveCommand = new RelayCommand<Data.Employee>(
                (s) => true,
                (s) =>
                {
                    if (lstEmployee.IndexOf(CurrentView) == -1) lstEmployee.Add(CurrentView);
                    if (IsCreating)
                    {
                        CurrentViewIndex = lstEmployee.IndexOf(CurrentView) + 1;
                        IsCreating = false;
                    }
                    if (CurrentView.Position.IsManager && CurrentView.Manager.ID != CurrentView.ID)
                    {
                        Messenger.Send(new Message.CannotChangeManager());
                        return;
                    }
                    CurrentView.Save();
                    IsEditable = false;
                }
            );
            AddPhotoCommand = new RelayCommand<object>(
                (s) => true,
                (s) =>
                {
                    CurrentView.AddPhoto();
                }
            );
            DeleteCommand = new RelayCommand<Data.Employee>(
                (s) => !IsViewModeEnable,
                (s) =>
                {
                    try
                    {
                        var message = new Message.DeleteConfirmation();
                        Messenger.Send(message);
                        if (message.Result == MessageBoxResult.No) return;

                        CurrentView.Delete();
                        lstEmployee.Remove(CurrentView);
                        Messenger.Send(new Message.BackConfimationOK());

                        IsViewModeEnable = true;
                        IsEditable = true;
                    }
                    catch
                    {
                        Messenger.Send(new Message.DeleteFailed());
                        Data.Execution.Restore();
                    }
                }
            );
            PreviousCommand = new RelayCommand<Data.Employee>(
                (s) => CurrentViewIndex > 1,
                (s) =>
                {
                    --CurrentViewIndex;
                }
            );
            NextCommand = new RelayCommand<Data.Employee>(
                (s) => CurrentViewIndex < lstEmployee.Count,
                (s) =>
                {
                    ++CurrentViewIndex;
                }
            );
            GoToPageCommand = new RelayCommand<Data.Employee>(
                (s) => true,
                (s) =>
                {
                    CurrentViewIndex = CurrentViewIndex;
                }
            );
            SearchFilter = new ObservableCollection<string>();            
            SearchFilterIndex = 0;
            SearchCommand = new RelayCommand<String>(
                (s) => true,
                (s) =>
                {
                    if (!IsSearching)
                    {
                        IsSearching = true;
                        return;
                    }
                    switch (SearchFilterIndex)
                    {
                        case 0: lstEmployee = Data.Employee.SearchByID(s);
                            break;
                        case 1: lstEmployee = Data.Employee.SearchByName(s);
                            break;
                    }
                }
            );
            SearchCancelCommand = new RelayCommand<object>(
                (s) => true,
                (s) =>
                {
                    IsSearching = false;
                    lstEmployee = Data.Employee.GetAllEmployee();
                }
            );
            AddDepartmentCommand = new RelayCommand<object>(
                (s) => !IsCreatingDepartment,
                (s) =>
                {
                    IsCreatingDepartment = true;
                }
            ); 
            AddPositionCommand = new RelayCommand<object>(
              (s) => !IsCreatingPosition,
              (s) =>
              {
                  IsCreatingPosition = true;
              }
            );
            ResetPasswordCommand = new RelayCommand<object>(
                (s) => !IsEditable && !IsViewModeEnable,
                (s) =>
                {
                    try
                    {
                        var message = new Message.ResetPasswordConfimation();
                        Messenger.Send(message);
                        if (message.Result == MessageBoxResult.No) return;

                        CurrentView.Password = CurrentView.CardNo + "";
                        CurrentView.Save();
                        Messenger.Send(new Message.ResetPasswordOK());
                    }
                    catch 
                    {
                        Messenger.Send(new Message.ResetPasswordFailed());
                    }
                    
                }
            ); 
        }
    }
}
